package com.qf.service.impl;

import com.qf.mapper.ProjectMapper;
import com.qf.model.Project;
import com.qf.page.PageResult;
import com.qf.page.QueryPageBean;
import com.qf.service.ProjectService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
@Service
public class ProjectServiceImpl implements ProjectService {
    @Autowired
    private ProjectMapper projectMapper;
    @Override
    public List<Project> getAll() {
        List<Project> projects = projectMapper.findAll();
        return projects;
    }

    @Override
    public PageResult findPage(QueryPageBean queryPageBean) {
        PageResult pageResult = new PageResult();
        Integer currentPage = queryPageBean.getCurrentPage(); //当前页
        Integer pageSize = queryPageBean.getPageSize();//每页大小
        //total 总记录数
        List<Project> page = projectMapper.findPage((currentPage - 1) * pageSize, pageSize);
        //rows  当前页的数据
        Long count = projectMapper.count();
        pageResult.setRows(page);
        pageResult.setTotal(count);
        return pageResult;
    }

    @Override
    @Transactional
    public int addProject(Project project) {
        project.setUpdatetime(new Date());
        int result = projectMapper.addProject(project);
        return result;
    }

    @Override
    public Project findByPid(int pid) {
        Project project = projectMapper.selectByPid(pid);
        return project;
    }

    @Override
    @Transactional
    public int edit(Project project) {
        project.setUpdatetime(new Date());
        int row = projectMapper.edit(project);
        return row;
    }

    @Override
    @Transactional
    public int deleteByPid(Integer pid) {
        int row = projectMapper.deleteByPid(pid);
        return row;
    }

    //绑定查询
    @Override
    @Transactional
    public void bindDid(String did, int[] pids) {
        if (pids != null && pids.length>0){
            for (int pid : pids) {
                projectMapper.bindDid(did,pid);
            }
        }
    }

    @Override
    public List<Project> findByDid(String did) {
        List<Project> list = projectMapper.findByDid(did);
        return list;
    }

    @Override
    @Transactional
    public void bindCid(String cid, int[] pids) {
        if (pids != null && pids.length>0){
            for (int pid : pids){
                projectMapper.bindCid(cid,pid);
            }
        }
    }

    @Override
    public List<Project> findByCid(String cid) {
        List<Project> list = projectMapper.findByCid(cid);
        return list;
    }

    @Override
    public void deleteByDid(String did) {
         projectMapper.deleteByDid(did);
    }

    @Override
    public void deleteByCid(String cid) {
        projectMapper.deleteByCid(cid);
    }

    @Override
    public void deleteDid(String did) {
        projectMapper.deleteDid(did);
    }

    @Override
    public void deleteCid(String cid) {
        projectMapper.deleteCid(cid);
    }

}
